// 需求相关 api
import { ajaxService } from '@/utils/request';
import { getToken } from '@/utils/auth';
import axios from 'axios';
import type {
  DemandSearch,
  DemandAdd,
  DemandDel,
  DemandUpdate,
  DemandNumber,
} from './type';

const env = import.meta.env;
// let baseURL = env.VITE_DEMAND_URL
let baseURL = env.VITE_ORDER_URL;

// 获取需求列表
export interface Demand {
  type: number;
}
export function getDemandList(params: Demand) {
  return ajaxService({
    url: '/order/design/getAll',
    method: 'get',
    params,
    noToken: false,
    baseURL,
  });
}
/**
 * 表单控件
 */
export interface Form {
  name: string;
  code: string;
  componentType: string;
  props: string;
  required: number;
  span: number;
  uuid: string;
  isMust: number;
  isProcessConditions: number;
}
/**
 * 表单
 * type：表单类型，1需求表单，2订单表单
 * name：表单名称
 * form：表单控件数据
 * print：打印配置
 */
export interface FormComponent {
  type: number;
  name: string;
  form: Form[];
  print: any;
}
// 添加
export function postOrderDesignAdd(data: FormComponent) {
  return ajaxService({
    url: '/order/design/add',
    method: 'post',
    data,
    noToken: false,
    baseURL,
  });
}
// 删除需求表单与打印
export interface DemandFormDel {
  id: number;
}
export function deleteOrderDesignDel(params: DemandFormDel) {
  return ajaxService({
    url: '/order/design/del',
    method: 'delete',
    params,
    noToken: false,
    baseURL,
  });
}
// 获取一份需求信息
export interface DemandFormInfo {
  id: number;
}
export function getDemandFromInfo(params: DemandFormInfo) {
  return ajaxService({
    url: '/order/design/get',
    method: 'get',
    params,
    noToken: false,
    baseURL,
  });
}
// 添加控件表单
export interface FormAdd {
  id: number;
  userId: number;
  name: string;
  checked: number;
  form: Form[];
  print: any;
}
// 修改
export function putOrderDesignMod(data: FormAdd) {
  return ajaxService({
    url: '/order/design/mod',
    method: 'post',
    data,
    noToken: false,
    baseURL,
  });
}

// 复制需求控件表单
export interface DemandFormCopy {
  id: number;
}
export function postCopyOrderDesign(data: DemandFormCopy) {
  return ajaxService({
    url: '/order/design/copy',
    method: 'post',
    data,
    noToken: false,
    baseURL,
  });
}
interface ScopeType {
  userStaffId: number;
  userDepartmentId: number;
}
// 需求模板数据类型，后端字段尚未完善
export interface DemandTemplate {
  type: number; // 1需求 2订单
  tempName: string;
  state: number;
  tempDes: string; // 单据说明
  icon: string;
  orderDesignId: number; // 需求模板id
  flowId: number;
  scopeType: number; // 适用范围 1.限定部门或人员适用 2.不限
  amountViweType: number; // 需求金额可见 1.仅当前录入人员和指定人员可见 2.不限
  scope: ScopeType[];
  amountView: ScopeType[];
}
// 添加需求模板
export function postDemandTemplate(data: DemandTemplate) {
  return ajaxService({
    url: '/order/template/add',
    method: 'post',
    data,
    noToken: false,
    baseURL,
  });
}

export interface DemandTemplateDel {
  id: number;
}
/**
 * 删除需求模板
 * @param data
 * @returns
 */
export function deleteDemandTemplate(params: DemandTemplateDel) {
  return ajaxService({
    url: '/order/template/del',
    method: 'delete',
    params,
    noToken: false,
    baseURL,
  });
}

export interface DemandTemplateUpdate {
  id?: number;
  type: number; // 1需求 2订单
  tempName: string;
  state: number;
  tempDes: string; // 单据说明
  icon: string;
  orderDesignId: number; // 需求模板id
  flowId: number;
  scopeType: number; // 适用范围 1.限定部门或人员适用 2.不限
  amountViweType: number; // 需求金额可见 1.仅当前录入人员和指定人员可见 2.不限
  scope: ScopeType[];
  amountView: ScopeType[];
}

/**
 * 修改需求模板
 * @param data k
 * @returns
 */
export function putDemandTemplate(data: DemandTemplateUpdate) {
  return ajaxService({
    url: '/order/template/mod',
    method: 'post',
    data,
    noToken: false,
    baseURL,
  });
}

export interface DemandTemplateList {
  type: number;
}
/**
 * 获取需求模板列表
 * @param data
 * @returns
 */
export function getDemandTemplateList(params: any) {
  return ajaxService({
    url: '/order/template/getAll',
    method: 'get',
    noToken: false,
    baseURL,
    params,
  });
}
export interface DemandTemplateInfo {
  id: number;
}
/**
 * 获取一份需求模板信息
 * @param data
 * @returns
 */
export function getDemandTemplateInfo(params: DemandTemplateInfo) {
  return ajaxService({
    url: '/order/template/get',
    method: 'get',
    params,
    noToken: false,
    baseURL,
  });
}
export interface StatusOpen {
  id: number;
}
/**
 * 需求模板状态开启，后端做了自动关闭其他模板状态
 * @param data
 * @returns
 */
export function postDemandTemplateStatus(data: StatusOpen) {
  return ajaxService({
    url: '/order/template/statusOpen',
    method: 'post',
    data,
    noToken: false,
    baseURL,
  });
}

/**
 * 客户需求列表 搜索
 * @param params
 * @returns
 */
export function postOrderDemandSearch(params: DemandSearch) {
  return ajaxService({
    url: '/order/demand/search',
    method: 'get',
    params,
    noToken: false,
    baseURL,
  });
}
interface TemplateStatus {
  type: number;
}
/**
 * 获取激活的模板表单
 * @param data
 * @returns
 */
export function getDemandFormStatusOn(data: TemplateStatus) {
  return ajaxService({
    url: '/order/template/getByTemplateStatusOn',
    method: 'post',
    data,
    noToken: false,
    baseURL,
  });
}
/**
 * 添加客户需求
 * @param data
 * @returns
 */
export function postDemandForm(data: DemandAdd) {
  return ajaxService({
    url: '/order/demand',
    method: 'post',
    data,
    noToken: false,
    baseURL,
  });
}
/**
 * 删除客户需求
 * @param data
 * @returns
 */
export function deleteDemandForm(data: DemandDel) {
  return ajaxService({
    url: '/order/demand',
    method: 'delete',
    data,
    noToken: false,
    baseURL,
  });
}

export function putDemandState(data: DemandDel) {
  return ajaxService({
    url: '/order/demand/state',
    method: 'put',
    data,
    noToken: false,
    baseURL,
  });
}
/**
 * 编辑客户需求
 * @param data
 * @returns
 */
export function putDemandForm(data: DemandUpdate) {
  return ajaxService({
    url: '/order/demand',
    method: 'put',
    data,
    noToken: false,
    baseURL,
  });
}
interface GetDemand {
  id: number;
}
/**
 * 获取客户需求
 * @param data
 * @returns
 */
export function getOrderDemand(data: GetDemand) {
  return ajaxService({
    url: '/order/demand',
    method: 'get',
    params: data,
    noToken: false,
    baseURL,
  });
}

/**
 * 需求编号设置
 * @param data
 * @returns
 */
export function postDemandNumberSetting(data: DemandNumber) {
  return ajaxService({
    url: '/order/demand/numSet',
    method: 'post',
    data,
    noToken: false,
    baseURL,
  });
}
/**
 * 获取需求编号设置
 */
export function getDemandNumberSetting() {
  return ajaxService({
    url: '/order/demand/numSet',
    method: 'get',
    noToken: false,
    baseURL,
  });
}
/**
 * 提交评审
 */
interface DemandStatus {
  id: number;
}
export function putDemandStatus(data: DemandStatus) {
  return ajaxService({
    url: '/order/demand/submitReview',
    method: 'put',
    noToken: false,
    data,
    baseURL,
  });
}

// 获取所有该我审核的需求列表
export function getFlowDemand(params: any) {
  return ajaxService({
    url: '/order/demand/flow',
    method: 'get',
    noToken: false,
    params,
    baseURL,
  });
}

// 获取所有该我审核的需求列表
export function putOrderDemandFlow(data: any) {
  return ajaxService({
    url: '/order/demand/flow',
    method: 'put',
    noToken: false,
    data,
    baseURL,
  });
}

// 导出需求数据
export async function GetDemandDeriveExcel(name?: string) {
  const res: any = await axios({
    method: 'get',
    url: '/order/demand/derive',
    baseURL,
    headers: {
      'Content-Type': 'multipart/form-data;charset=UTF-8',
      'Authorization': `${getToken()}`,
    },
    responseType: 'blob',
    // data: file,
  });
  const file = new Blob([res.data], { type: 'application/xlsx' });
  const changeUrl = window.webkitURL.createObjectURL(file);
  let downloadElement: any = document.createElement('a');
  downloadElement.download = (name || 'test') + '.xlsx';
  downloadElement.href = changeUrl;
  downloadElement.click();
  downloadElement = undefined;
  window.URL.revokeObjectURL(changeUrl);
}
